package com.gxa.jjty.admin.controller;

import com.github.pagehelper.PageInfo;
import com.gxa.jjty.admin.service.ReturnVOService;
import com.gxa.jjty.core.common.JsonResult;
import com.gxa.jjty.core.pojo.ReturnService;
import com.gxa.jjty.core.pojo.ReturnVO;
import com.gxa.jjty.core.util.StringUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Timestamp;
import java.util.List;
import java.util.Map;

/**
 * @ClassName ReturnVOController
 * @Author: ws
 * @date 2021/12/9 16:59
 */
@Log4j2
@RestController
@RequestMapping("/returnVO")
@Api(tags = "回访服务的接口")
public class ReturnVOController {

    @Autowired
    private ReturnVOService returnVOService;


    @GetMapping("/getAll")
    @ApiOperation("分页全查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page",value = "页数",dataType = "int",paramType = "query"),
            @ApiImplicitParam(name = "size",value = "每页的记录数",dataType = "int",paramType = "query"),
    })
    public Map getAll(@RequestParam("page") Integer page, @RequestParam("size") Integer size) throws IllegalAccessException {

        PageInfo<ReturnVO> pageInfo = returnVOService.selectByPage(page, size);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(pageInfo.getList());
        jsonResult.put("total",pageInfo.getTotal());

        return jsonResult.getDataMap();
    }

    @GetMapping("/getAllBy")
    @ApiOperation("分页全/带条件模糊查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数",paramType = "query",dataType = "int"),
            @ApiImplicitParam(name = "size", value = "每页记录数",paramType = "query",dataType = "int"),
            @ApiImplicitParam(name = "from", value = "起始时间",paramType = "query",dataType = "time"),
            @ApiImplicitParam(name = "to", value = "结束时间",paramType = "query",dataType = "time"),
            @ApiImplicitParam(name = "name", value = "下单人姓名",paramType = "query",dataType = "String"),
            @ApiImplicitParam(name = "state", value = "状态",paramType = "query",dataType = "String"),
    })
    public Map getByTimeAnd(@RequestParam("page") Integer page, @RequestParam("size") Integer size,
                            @RequestParam(value = "from",required = false) String from,
                            @RequestParam(value = "to",required = false) String to,
                            @RequestParam(value = "name",required = false) String name,
                            @RequestParam(value = "state",required = false) String state
                            ) throws IllegalAccessException {

        Timestamp newFrom = null;
        Timestamp newTo = null;
        Boolean newState = null;

        if (StringUtil.isEmpty(from)) {
            newFrom = new Timestamp(0);
        }else {
            newFrom = Timestamp.valueOf(from);
        }
        if (StringUtil.isEmpty(to)) {
            newTo = new Timestamp(System.currentTimeMillis());
        }else {
            newTo = Timestamp.valueOf(to);
        }
        if ("true".equals(state)){
            newState = true;
        }
        if ("false".equals(state)){
            newState = false;
        }

        PageInfo<ReturnVO> pageInfo = returnVOService.selectByTimeAnd(page, size, newFrom, newTo, name,newState);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(pageInfo.getList());
        jsonResult.put("total",pageInfo.getTotal());

        return jsonResult.getDataMap();
    }

    @GetMapping("/getCount")
    @ApiOperation("获取当前订单总数/回访数/回访率/满意度")
    public Map getCount(){

        Map map = returnVOService.selectCount();

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(map);

        return jsonResult.getDataMap();
    }

    @PostMapping("/update")
    @ApiOperation("回访操作")
    @ApiImplicitParam(name = "returnService",value = "{\"orderId\":\"\",\"content\":\"\",\"satisfaction\":\"\"}" +
            "",paramType = "body",dataType = "ReturnService")
    // {"orderId":"","content":"","satisfaction":""}
    public Map update(@RequestBody ReturnService returnService){

        System.out.println("returnService: "+returnService);
        Integer integer = returnVOService.updateReturn(returnService);

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setMsg("回访成功");

        return jsonResult.getDataMap();
    }

    @GetMapping("getReturn")
    @ApiOperation("查看回访记录")
    @ApiImplicitParam(name = "orderId",value = "订单的id",paramType = "query",dataType = "int")
    public Map getReturnService( Long orderId){

        ReturnService returnService = new ReturnService();
        returnService.setOrderId(orderId);
        List<ReturnService> returnServices = returnVOService.getReturnService(returnService);

        ReturnService returnService1 = null;
        if (returnServices.size() > 0) {
            returnService1 = returnServices.get(0);
        }

        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("1");
        jsonResult.setData(returnService1);

        return jsonResult.getDataMap();
    }


}
